home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / perl / 5.10.1 / Fatal.pm < prev    next >
Encoding:
Perl POD Document  |  2012-12-11  |  40.5 KB  |  1,387 lines

  1. package Fatal;
  2.  
  3. use 5.008;  # 5.8.x needed for autodie
  4. use Carp;
  5. use strict;
  6. use warnings;
  7. use Tie::RefHash;   # To cache subroutine refs
  8. use Config;
  9.  
  10. use constant PERL510     => ( $] >= 5.010 );
  11.  
  12. use constant LEXICAL_TAG => q{:lexical};
  13. use constant VOID_TAG    => q{:void};
  14. use constant INSIST_TAG  => q{!};
  15.  
  16. use constant ERROR_NOARGS    => 'Cannot use lexical %s with no arguments';
  17. use constant ERROR_VOID_LEX  => VOID_TAG.' cannot be used with lexical scope';
  18. use constant ERROR_LEX_FIRST => LEXICAL_TAG.' must be used as first argument';
  19. use constant ERROR_NO_LEX    => "no %s can only start with ".LEXICAL_TAG;
  20. use constant ERROR_BADNAME   => "Bad subroutine name for %s: %s";
  21. use constant ERROR_NOTSUB    => "%s is not a Perl subroutine";
  22. use constant ERROR_NOT_BUILT => "%s is neither a builtin, nor a Perl subroutine";
  23. use constant ERROR_NOHINTS   => "No user hints defined for %s";
  24.  
  25. use constant ERROR_CANT_OVERRIDE => "Cannot make the non-overridable builtin %s fatal";
  26.  
  27. use constant ERROR_NO_IPC_SYS_SIMPLE => "IPC::System::Simple required for Fatalised/autodying system()";
  28.  
  29. use constant ERROR_IPC_SYS_SIMPLE_OLD => "IPC::System::Simple version %f required for Fatalised/autodying system().  We only have version %f";
  30.  
  31. use constant ERROR_AUTODIE_CONFLICT => q{"no autodie '%s'" is not allowed while "use Fatal '%s'" is in effect};
  32.  
  33. use constant ERROR_FATAL_CONFLICT => q{"use Fatal '%s'" is not allowed while "no autodie '%s'" is in effect};
  34.  
  35. use constant ERROR_58_HINTS => q{Non-subroutine %s hints for %s are not supported under Perl 5.8.x};
  36.  
  37. # Older versions of IPC::System::Simple don't support all the
  38. # features we need.
  39.  
  40. use constant MIN_IPC_SYS_SIMPLE_VER => 0.12;
  41.  
  42. # All the Fatal/autodie modules share the same version number.
  43. our $VERSION = '2.06_01';
  44.  
  45. our $Debug ||= 0;
  46.  
  47. # EWOULDBLOCK values for systems that don't supply their own.
  48. # Even though this is defined with our, that's to help our
  49. # test code.  Please don't rely upon this variable existing in
  50. # the future.
  51.  
  52. our %_EWOULDBLOCK = (
  53.     MSWin32 => 33,
  54. );
  55.  
  56. # the linux parisc port has separate EAGAIN and EWOULDBLOCK,
  57. # and the kernel returns EAGAIN
  58. my $try_EAGAIN = ($^O eq 'linux' and $Config{archname} =~ /hppa|parisc/) ? 1 : 0;
  59.  
  60. # We have some tags that can be passed in for use with import.
  61. # These are all assumed to be CORE::
  62.  
  63. my %TAGS = (
  64.     ':io'      => [qw(:dbm :file :filesys :ipc :socket
  65.                        read seek sysread syswrite sysseek )],
  66.     ':dbm'     => [qw(dbmopen dbmclose)],
  67.     ':file'    => [qw(open close flock sysopen fcntl fileno binmode
  68.                      ioctl truncate)],
  69.     ':filesys' => [qw(opendir closedir chdir link unlink rename mkdir
  70.                       symlink rmdir readlink umask)],
  71.     ':ipc'     => [qw(:msg :semaphore :shm pipe)],
  72.     ':msg'     => [qw(msgctl msgget msgrcv msgsnd)],
  73.     ':threads' => [qw(fork)],
  74.     ':semaphore'=>[qw(semctl semget semop)],
  75.     ':shm'     => [qw(shmctl shmget shmread)],
  76.     ':system'  => [qw(system exec)],
  77.  
  78.     # Can we use qw(getpeername getsockname)? What do they do on failure?
  79.     # TODO - Can socket return false?
  80.     ':socket'  => [qw(accept bind connect getsockopt listen recv send
  81.                    setsockopt shutdown socketpair)],
  82.  
  83.     # Our defaults don't include system(), because it depends upon
  84.     # an optional module, and it breaks the exotic form.
  85.     #
  86.     # This *may* change in the future.  I'd love IPC::System::Simple
  87.     # to be a dependency rather than a recommendation, and hence for
  88.     # system() to be autodying by default.
  89.  
  90.     ':default' => [qw(:io :threads)],
  91.  
  92.     # Version specific tags.  These allow someone to specify
  93.     # use autodie qw(:1.994) and know exactly what they'll get.
  94.  
  95.     ':1.994' => [qw(:default)],
  96.     ':1.995' => [qw(:default)],
  97.     ':1.996' => [qw(:default)],
  98.     ':1.997' => [qw(:default)],
  99.     ':1.998' => [qw(:default)],
  100.     ':1.999' => [qw(:default)],
  101.     ':1.999_01' => [qw(:default)],
  102.     ':2.00'  => [qw(:default)],
  103.     ':2.01'  => [qw(:default)],
  104.     ':2.02'  => [qw(:default)],
  105.     ':2.03'  => [qw(:default)],
  106.     ':2.04'  => [qw(:default)],
  107.     ':2.05'  => [qw(:default)],
  108.     ':2.06'  => [qw(:default)],
  109.     ':2.06_01' => [qw(:default)],
  110. );
  111.  
  112. $TAGS{':all'}  = [ keys %TAGS ];
  113.  
  114. # This hash contains subroutines for which we should
  115. # subroutine() // die() rather than subroutine() || die()
  116.  
  117. my %Use_defined_or;
  118.  
  119. # CORE::open returns undef on failure.  It can legitimately return
  120. # 0 on success, eg: open(my $fh, '-|') || exec(...);
  121.  
  122. @Use_defined_or{qw(
  123.     CORE::fork
  124.     CORE::recv
  125.     CORE::send
  126.     CORE::open
  127.     CORE::fileno
  128.     CORE::read
  129.     CORE::readlink
  130.     CORE::sysread
  131.     CORE::syswrite
  132.     CORE::sysseek
  133.     CORE::umask
  134. )} = ();
  135.  
  136. # Cached_fatalised_sub caches the various versions of our
  137. # fatalised subs as they're produced.  This means we don't
  138. # have to build our own replacement of CORE::open and friends
  139. # for every single package that wants to use them.
  140.  
  141. my %Cached_fatalised_sub = ();
  142.  
  143. # Every time we're called with package scope, we record the subroutine
  144. # (including package or CORE::) in %Package_Fatal.  This allows us
  145. # to detect illegal combinations of autodie and Fatal, and makes sure
  146. # we don't accidently make a Fatal function autodying (which isn't
  147. # very useful).
  148.  
  149. my %Package_Fatal = ();
  150.  
  151. # The first time we're called with a user-sub, we cache it here.
  152. # In the case of a "no autodie ..." we put back the cached copy.
  153.  
  154. my %Original_user_sub = ();
  155.  
  156. # Is_fatalised_sub simply records a big map of fatalised subroutine
  157. # refs.  It means we can avoid repeating work, or fatalising something
  158. # we've already processed.
  159.  
  160. my  %Is_fatalised_sub = ();
  161. tie %Is_fatalised_sub, 'Tie::RefHash';
  162.  
  163. # We use our package in a few hash-keys.  Having it in a scalar is
  164. # convenient.  The "guard $PACKAGE" string is used as a key when
  165. # setting up lexical guards.
  166.  
  167. my $PACKAGE       = __PACKAGE__;
  168. my $PACKAGE_GUARD = "guard $PACKAGE";
  169. my $NO_PACKAGE    = "no $PACKAGE";      # Used to detect 'no autodie'
  170.  
  171. # Here's where all the magic happens when someone write 'use Fatal'
  172. # or 'use autodie'.
  173.  
  174. sub import {
  175.     my $class        = shift(@_);
  176.     my $void         = 0;
  177.     my $lexical      = 0;
  178.     my $insist_hints = 0;
  179.  
  180.     my ($pkg, $filename) = caller();
  181.  
  182.     @_ or return;   # 'use Fatal' is a no-op.
  183.  
  184.     # If we see the :lexical flag, then _all_ arguments are
  185.     # changed lexically
  186.  
  187.     if ($_[0] eq LEXICAL_TAG) {
  188.         $lexical = 1;
  189.         shift @_;
  190.  
  191.         # If we see no arguments and :lexical, we assume they
  192.         # wanted ':default'.
  193.  
  194.         if (@_ == 0) {
  195.             push(@_, ':default');
  196.         }
  197.  
  198.         # Don't allow :lexical with :void, it's needlessly confusing.
  199.         if ( grep { $_ eq VOID_TAG } @_ ) {
  200.             croak(ERROR_VOID_LEX);
  201.         }
  202.     }
  203.  
  204.     if ( grep { $_ eq LEXICAL_TAG } @_ ) {
  205.         # If we see the lexical tag as the non-first argument, complain.
  206.         croak(ERROR_LEX_FIRST);
  207.     }
  208.  
  209.     my @fatalise_these =  @_;
  210.  
  211.     # Thiese subs will get unloaded at the end of lexical scope.
  212.     my %unload_later;
  213.  
  214.     # This hash helps us track if we've alredy done work.
  215.     my %done_this;
  216.  
  217.     # NB: we're using while/shift rather than foreach, since
  218.     # we'll be modifying the array as we walk through it.
  219.  
  220.     while (my $func = shift @fatalise_these) {
  221.  
  222.         if ($func eq VOID_TAG) {
  223.  
  224.             # When we see :void, set the void flag.
  225.             $void = 1;
  226.  
  227.         } elsif ($func eq INSIST_TAG) {
  228.  
  229.             $insist_hints = 1;
  230.  
  231.         } elsif (exists $TAGS{$func}) {
  232.  
  233.             # When it's a tag, expand it.
  234.             push(@fatalise_these, @{ $TAGS{$func} });
  235.  
  236.         } else {
  237.  
  238.             # Otherwise, fatalise it.
  239.  
  240.             # Check to see if there's an insist flag at the front.
  241.             # If so, remove it, and insist we have hints for this sub.
  242.             my $insist_this;
  243.  
  244.             if ($func =~ s/^!//) {
  245.                 $insist_this = 1;
  246.             }
  247.  
  248.             # TODO: Even if we've already fatalised, we should
  249.             # check we've done it with hints (if $insist_hints).
  250.  
  251.             # If we've already made something fatal this call,
  252.             # then don't do it twice.
  253.  
  254.             next if $done_this{$func};
  255.  
  256.             # We're going to make a subroutine fatalistic.
  257.             # However if we're being invoked with 'use Fatal qw(x)'
  258.             # and we've already been called with 'no autodie qw(x)'
  259.             # in the same scope, we consider this to be an error.
  260.             # Mixing Fatal and autodie effects was considered to be
  261.             # needlessly confusing on p5p.
  262.  
  263.             my $sub = $func;
  264.             $sub = "${pkg}::$sub" unless $sub =~ /::/;
  265.  
  266.             # If we're being called as Fatal, and we've previously
  267.             # had a 'no X' in scope for the subroutine, then complain
  268.             # bitterly.
  269.  
  270.             if (! $lexical and $^H{$NO_PACKAGE}{$sub}) {
  271.                  croak(sprintf(ERROR_FATAL_CONFLICT, $func, $func));
  272.             }
  273.  
  274.             # We're not being used in a confusing way, so make
  275.             # the sub fatal.  Note that _make_fatal returns the
  276.             # old (original) version of the sub, or undef for
  277.             # built-ins.
  278.  
  279.             my $sub_ref = $class->_make_fatal(
  280.                 $func, $pkg, $void, $lexical, $filename,
  281.                 ( $insist_this || $insist_hints )
  282.             );
  283.  
  284.             $done_this{$func}++;
  285.  
  286.             $Original_user_sub{$sub} ||= $sub_ref;
  287.  
  288.             # If we're making lexical changes, we need to arrange
  289.             # for them to be cleaned at the end of our scope, so
  290.             # record them here.
  291.  
  292.             $unload_later{$func} = $sub_ref if $lexical;
  293.         }
  294.     }
  295.  
  296.     if ($lexical) {
  297.  
  298.         # Dark magic to have autodie work under 5.8
  299.         # Copied from namespace::clean, that copied it from
  300.         # autobox, that found it on an ancient scroll written
  301.         # in blood.
  302.  
  303.         # This magic bit causes %^H to be lexically scoped.
  304.  
  305.         $^H |= 0x020000;
  306.  
  307.         # Our package guard gets invoked when we leave our lexical
  308.         # scope.
  309.  
  310.         push(@ { $^H{$PACKAGE_GUARD} }, autodie::Scope::Guard->new(sub {
  311.             $class->_install_subs($pkg, \%unload_later);
  312.         }));
  313.  
  314.     }
  315.  
  316.     return;
  317.  
  318. }
  319.  
  320. # The code here is originally lifted from namespace::clean,
  321. # by Robert "phaylon" Sedlacek.
  322. #
  323. # It's been redesigned after feedback from ikegami on perlmonks.
  324. # See http://perlmonks.org/?node_id=693338 .  Ikegami rocks.
  325. #
  326. # Given a package, and hash of (subname => subref) pairs,
  327. # we install the given subroutines into the package.  If
  328. # a subref is undef, the subroutine is removed.  Otherwise
  329. # it replaces any existing subs which were already there.
  330.  
  331. sub _install_subs {
  332.     my ($class, $pkg, $subs_to_reinstate) = @_;
  333.  
  334.     my $pkg_sym = "${pkg}::";
  335.  
  336.     while(my ($sub_name, $sub_ref) = each %$subs_to_reinstate) {
  337.  
  338.         my $full_path = $pkg_sym.$sub_name;
  339.  
  340.         # Copy symbols across to temp area.
  341.  
  342.         no strict 'refs';   ## no critic
  343.  
  344.         local *__tmp = *{ $full_path };
  345.  
  346.         # Nuke the old glob.
  347.         { no strict; delete $pkg_sym->{$sub_name}; }    ## no critic
  348.  
  349.         # Copy innocent bystanders back.  Note that we lose
  350.         # formats; it seems that Perl versions up to 5.10.0
  351.         # have a bug which causes copying formats to end up in
  352.         # the scalar slot.  Thanks to Ben Morrow for spotting this.
  353.  
  354.         foreach my $slot (qw( SCALAR ARRAY HASH IO ) ) {
  355.             next unless defined *__tmp{ $slot };
  356.             *{ $full_path } = *__tmp{ $slot };
  357.         }
  358.  
  359.         # Put back the old sub (if there was one).
  360.  
  361.         if ($sub_ref) {
  362.  
  363.             no strict;  ## no critic
  364.             *{ $pkg_sym . $sub_name } = $sub_ref;
  365.         }
  366.     }
  367.  
  368.     return;
  369. }
  370.  
  371. sub unimport {
  372.     my $class = shift;
  373.  
  374.     # Calling "no Fatal" must start with ":lexical"
  375.     if ($_[0] ne LEXICAL_TAG) {
  376.         croak(sprintf(ERROR_NO_LEX,$class));
  377.     }
  378.  
  379.     shift @_;   # Remove :lexical
  380.  
  381.     my $pkg = (caller)[0];
  382.  
  383.     # If we've been called with arguments, then the developer
  384.     # has explicitly stated 'no autodie qw(blah)',
  385.     # in which case, we disable Fatalistic behaviour for 'blah'.
  386.  
  387.     my @unimport_these = @_ ? @_ : ':all';
  388.  
  389.     while (my $symbol = shift @unimport_these) {
  390.  
  391.         if ($symbol =~ /^:/) {
  392.  
  393.             # Looks like a tag!  Expand it!
  394.             push(@unimport_these, @{ $TAGS{$symbol} });
  395.  
  396.             next;
  397.         }
  398.  
  399.         my $sub = $symbol;
  400.         $sub = "${pkg}::$sub" unless $sub =~ /::/;
  401.  
  402.         # If 'blah' was already enabled with Fatal (which has package
  403.         # scope) then, this is considered an error.
  404.  
  405.         if (exists $Package_Fatal{$sub}) {
  406.             croak(sprintf(ERROR_AUTODIE_CONFLICT,$symbol,$symbol));
  407.         }
  408.  
  409.         # Record 'no autodie qw($sub)' as being in effect.
  410.         # This is to catch conflicting semantics elsewhere
  411.         # (eg, mixing Fatal with no autodie)
  412.  
  413.         $^H{$NO_PACKAGE}{$sub} = 1;
  414.  
  415.         if (my $original_sub = $Original_user_sub{$sub}) {
  416.             # Hey, we've got an original one of these, put it back.
  417.             $class->_install_subs($pkg, { $symbol => $original_sub });
  418.             next;
  419.         }
  420.  
  421.         # We don't have an original copy of the sub, on the assumption
  422.         # it's core (or doesn't exist), we'll just nuke it.
  423.  
  424.         $class->_install_subs($pkg,{ $symbol => undef });
  425.  
  426.     }
  427.  
  428.     return;
  429.  
  430. }
  431.  
  432. # TODO - This is rather terribly inefficient right now.
  433.  
  434. # NB: Perl::Critic's dump-autodie-tag-contents depends upon this
  435. # continuing to work.
  436.  
  437. {
  438.     my %tag_cache;
  439.  
  440.     sub _expand_tag {
  441.         my ($class, $tag) = @_;
  442.  
  443.         if (my $cached = $tag_cache{$tag}) {
  444.             return $cached;
  445.         }
  446.  
  447.         if (not exists $TAGS{$tag}) {
  448.             croak "Invalid exception class $tag";
  449.         }
  450.  
  451.         my @to_process = @{$TAGS{$tag}};
  452.  
  453.         my @taglist = ();
  454.  
  455.         while (my $item = shift @to_process) {
  456.             if ($item =~ /^:/) {
  457.                 push(@to_process, @{$TAGS{$item}} );
  458.             } else {
  459.                 push(@taglist, "CORE::$item");
  460.             }
  461.         }
  462.  
  463.         $tag_cache{$tag} = \@taglist;
  464.  
  465.         return \@taglist;
  466.  
  467.     }
  468.  
  469. }
  470.  
  471. # This code is from the original Fatal.  It scares me.
  472. # It is 100% compatible with the 5.10.0 Fatal module, right down
  473. # to the scary 'XXXX' comment.  ;)
  474.  
  475. sub fill_protos {
  476.     my $proto = shift;
  477.     my ($n, $isref, @out, @out1, $seen_semi) = -1;
  478.     while ($proto =~ /\S/) {
  479.         $n++;
  480.         push(@out1,[$n,@out]) if $seen_semi;
  481.         push(@out, $1 . "{\$_[$n]}"), next if $proto =~ s/^\s*\\([\@%\$\&])//;
  482.         push(@out, "\$_[$n]"),        next if $proto =~ s/^\s*([_*\$&])//;
  483.         push(@out, "\@_[$n..\$#_]"),  last if $proto =~ s/^\s*(;\s*)?\@//;
  484.         $seen_semi = 1, $n--,         next if $proto =~ s/^\s*;//; # XXXX ????
  485.         die "Internal error: Unknown prototype letters: \"$proto\"";
  486.     }
  487.     push(@out1,[$n+1,@out]);
  488.     return @out1;
  489. }
  490.  
  491. # This is a backwards compatible version of _write_invocation.  It's
  492. # recommended you don't use it.
  493.  
  494. sub write_invocation {
  495.     my ($core, $call, $name, $void, @args) = @_;
  496.  
  497.     return Fatal->_write_invocation(
  498.         $core, $call, $name, $void,
  499.         0,      # Lexical flag
  500.         undef,  # Sub, unused in legacy mode
  501.         undef,  # Subref, unused in legacy mode.
  502.         @args
  503.     );
  504. }
  505.  
  506. # This version of _write_invocation is used internally.  It's not
  507. # recommended you call it from external code, as the interface WILL
  508. # change in the future.
  509.  
  510. sub _write_invocation {
  511.  
  512.     my ($class, $core, $call, $name, $void, $lexical, $sub, $sref, @argvs) = @_;
  513.  
  514.     if (@argvs == 1) {        # No optional arguments
  515.  
  516.         my @argv = @{$argvs[0]};
  517.         shift @argv;
  518.  
  519.         return $class->_one_invocation($core,$call,$name,$void,$sub,! $lexical, $sref, @argv);
  520.  
  521.     } else {
  522.         my $else = "\t";
  523.         my (@out, @argv, $n);
  524.         while (@argvs) {
  525.             @argv = @{shift @argvs};
  526.             $n = shift @argv;
  527.  
  528.             push @out, "${else}if (\@_ == $n) {\n";
  529.             $else = "\t} els";
  530.  
  531.         push @out, $class->_one_invocation($core,$call,$name,$void,$sub,! $lexical, $sref, @argv);
  532.         }
  533.         push @out, qq[
  534.             }
  535.             die "Internal error: $name(\@_): Do not expect to get ", scalar(\@_), " arguments";
  536.     ];
  537.  
  538.         return join '', @out;
  539.     }
  540. }
  541.  
  542.  
  543. # This is a slim interface to ensure backward compatibility with
  544. # anyone doing very foolish things with old versions of Fatal.
  545.  
  546. sub one_invocation {
  547.     my ($core, $call, $name, $void, @argv) = @_;
  548.  
  549.     return Fatal->_one_invocation(
  550.         $core, $call, $name, $void,
  551.         undef,   # Sub.  Unused in back-compat mode.
  552.         1,       # Back-compat flag
  553.         undef,   # Subref, unused in back-compat mode.
  554.         @argv
  555.     );
  556.  
  557. }
  558.  
  559. # This is the internal interface that generates code.
  560. # NOTE: This interface WILL change in the future.  Please do not
  561. # call this subroutine directly.
  562.  
  563. # TODO: Whatever's calling this code has already looked up hints.  Pass
  564. # them in, rather than look them up a second time.
  565.  
  566. sub _one_invocation {
  567.     my ($class, $core, $call, $name, $void, $sub, $back_compat, $sref, @argv) = @_;
  568.  
  569.  
  570.     # If someone is calling us directly (a child class perhaps?) then
  571.     # they could try to mix void without enabling backwards
  572.     # compatibility.  We just don't support this at all, so we gripe
  573.     # about it rather than doing something unwise.
  574.  
  575.     if ($void and not $back_compat) {
  576.         Carp::confess("Internal error: :void mode not supported with $class");
  577.     }
  578.  
  579.     # @argv only contains the results of the in-built prototype
  580.     # function, and is therefore safe to interpolate in the
  581.     # code generators below.
  582.  
  583.     # TODO - The following clobbers context, but that's what the
  584.     #        old Fatal did.  Do we care?
  585.  
  586.     if ($back_compat) {
  587.  
  588.         # Use Fatal qw(system) will never be supported.  It generated
  589.         # a compile-time error with legacy Fatal, and there's no reason
  590.         # to support it when autodie does a better job.
  591.  
  592.         if ($call eq 'CORE::system') {
  593.             return q{
  594.                 croak("UNIMPLEMENTED: use Fatal qw(system) not supported.");
  595.             };
  596.         }
  597.  
  598.         local $" = ', ';
  599.  
  600.         if ($void) {
  601.             return qq/return (defined wantarray)?$call(@argv):
  602.                    $call(@argv) || croak "Can't $name(\@_)/ .
  603.                    ($core ? ': $!' : ', \$! is \"$!\"') . '"'
  604.         } else {
  605.             return qq{return $call(@argv) || croak "Can't $name(\@_)} .
  606.                    ($core ? ': $!' : ', \$! is \"$!\"') . '"';
  607.         }
  608.     }
  609.  
  610.     # The name of our original function is:
  611.     #   $call if the function is CORE
  612.     #   $sub if our function is non-CORE
  613.  
  614.     # The reason for this is that $call is what we're actualling
  615.     # calling.  For our core functions, this is always
  616.     # CORE::something.  However for user-defined subs, we're about to
  617.     # replace whatever it is that we're calling; as such, we actually
  618.     # calling a subroutine ref.
  619.  
  620.     my $human_sub_name = $core ? $call : $sub;
  621.  
  622.     # Should we be testing to see if our result is defined, or
  623.     # just true?
  624.  
  625.     my $use_defined_or;
  626.  
  627.     my $hints;      # All user-sub hints, including list hints.
  628.  
  629.     if ( $core ) {
  630.  
  631.         # Core hints are built into autodie.
  632.  
  633.         $use_defined_or = exists ( $Use_defined_or{$call} );
  634.  
  635.     }
  636.     else {
  637.  
  638.         # User sub hints are looked up using autodie::hints,
  639.         # since users may wish to add their own hints.
  640.  
  641.         require autodie::hints;
  642.  
  643.         $hints = autodie::hints->get_hints_for( $sref );
  644.  
  645.         # We'll look up the sub's fullname.  This means we
  646.         # get better reports of where it came from in our
  647.         # error messages, rather than what imported it.
  648.  
  649.         $human_sub_name = autodie::hints->sub_fullname( $sref );
  650.  
  651.     }
  652.  
  653.     # Checks for special core subs.
  654.  
  655.     if ($call eq 'CORE::system') {
  656.  
  657.         # Leverage IPC::System::Simple if we're making an autodying
  658.         # system.
  659.  
  660.         local $" = ", ";
  661.  
  662.         # We need to stash $@ into $E, rather than using
  663.         # local $@ for the whole sub.  If we don't then
  664.         # any exceptions from internal errors in autodie/Fatal
  665.         # will mysteriously disappear before propogating
  666.         # upwards.
  667.  
  668.         return qq{
  669.             my \$retval;
  670.             my \$E;
  671.  
  672.  
  673.             {
  674.                 local \$@;
  675.  
  676.                 eval {
  677.                     \$retval = IPC::System::Simple::system(@argv);
  678.                 };
  679.  
  680.                 \$E = \$@;
  681.             }
  682.  
  683.             if (\$E) {
  684.  
  685.                 # TODO - This can't be overridden in child
  686.                 # classes!
  687.  
  688.                 die autodie::exception::system->new(
  689.                     function => q{CORE::system}, args => [ @argv ],
  690.                     message => "\$E", errno => \$!,
  691.                 );
  692.             }
  693.  
  694.             return \$retval;
  695.         };
  696.  
  697.     }
  698.  
  699.     local $" = ', ';
  700.  
  701.     # If we're going to throw an exception, here's the code to use.
  702.     my $die = qq{
  703.         die $class->throw(
  704.             function => q{$human_sub_name}, args => [ @argv ],
  705.             pragma => q{$class}, errno => \$!,
  706.             context => \$context, return => \$retval,
  707.             eval_error => \$@
  708.         )
  709.     };
  710.  
  711.     if ($call eq 'CORE::flock') {
  712.  
  713.         # flock needs special treatment.  When it fails with
  714.         # LOCK_UN and EWOULDBLOCK, then it's not really fatal, it just
  715.         # means we couldn't get the lock right now.
  716.  
  717.         require POSIX;      # For POSIX::EWOULDBLOCK
  718.  
  719.         local $@;   # Don't blat anyone else's $@.
  720.  
  721.         # Ensure that our vendor supports EWOULDBLOCK.  If they
  722.         # don't (eg, Windows), then we use known values for its
  723.         # equivalent on other systems.
  724.  
  725.         my $EWOULDBLOCK = eval { POSIX::EWOULDBLOCK(); }
  726.                           || $_EWOULDBLOCK{$^O}
  727.                           || _autocroak("Internal error - can't overload flock - EWOULDBLOCK not defined on this system.");
  728.         my $EAGAIN = $EWOULDBLOCK;
  729.         if ($try_EAGAIN) {
  730.             $EAGAIN = eval { POSIX::EAGAIN(); }
  731.                           || _autocroak("Internal error - can't overload flock - EAGAIN not defined on this system.");
  732.         }
  733.  
  734.         require Fcntl;      # For Fcntl::LOCK_NB
  735.  
  736.         return qq{
  737.  
  738.             my \$context = wantarray() ? "list" : "scalar";
  739.  
  740.             # Try to flock.  If successful, return it immediately.
  741.  
  742.             my \$retval = $call(@argv);
  743.             return \$retval if \$retval;
  744.  
  745.             # If we failed, but we're using LOCK_NB and
  746.             # returned EWOULDBLOCK, it's not a real error.
  747.  
  748.             if (\$_[1] & Fcntl::LOCK_NB() and
  749.                 (\$! == $EWOULDBLOCK or
  750.                 ($try_EAGAIN and \$! == $EAGAIN ))) {
  751.                 return \$retval;
  752.             }
  753.  
  754.             # Otherwise, we failed.  Die noisily.
  755.  
  756.             $die;
  757.  
  758.         };
  759.     }
  760.  
  761.     # AFAIK everything that can be given an unopned filehandle
  762.     # will fail if it tries to use it, so we don't really need
  763.     # the 'unopened' warning class here.  Especially since they
  764.     # then report the wrong line number.
  765.  
  766.     # Other warnings are disabled because they produce excessive
  767.     # complaints from smart-match hints under 5.10.1.
  768.  
  769.     my $code = qq[
  770.         no warnings qw(unopened uninitialized numeric);
  771.  
  772.         if (wantarray) {
  773.             my \@results = $call(@argv);
  774.             my \$retval  = \\\@results;
  775.             my \$context = "list";
  776.  
  777.     ];
  778.  
  779.     if ( $hints and ( ref($hints->{list} ) || "" ) eq 'CODE' ) {
  780.  
  781.         # NB: Subroutine hints are passed as a full list.
  782.         # This differs from the 5.10.0 smart-match behaviour,
  783.         # but means that context unaware subroutines can use
  784.         # the same hints in both list and scalar context.
  785.  
  786.         $code .= qq{
  787.             if ( \$hints->{list}->(\@results) ) { $die };
  788.         };
  789.     }
  790.     elsif ( PERL510 and $hints ) {
  791.         $code .= qq{
  792.             if ( \@results ~~ \$hints->{list} ) { $die };
  793.         };
  794.     }
  795.     elsif ( $hints ) {
  796.         croak sprintf(ERROR_58_HINTS, 'list', $sub);
  797.     }
  798.     else {
  799.         $code .= qq{
  800.             # An empty list, or a single undef is failure
  801.             if (! \@results or (\@results == 1 and ! defined \$results[0])) {
  802.                 $die;
  803.             }
  804.         }
  805.     }
  806.  
  807.     # Tidy up the end of our wantarray call.
  808.  
  809.     $code .= qq[
  810.             return \@results;
  811.         }
  812.     ];
  813.  
  814.  
  815.     # Otherwise, we're in scalar context.
  816.     # We're never in a void context, since we have to look
  817.     # at the result.
  818.  
  819.     $code .= qq{
  820.         my \$retval  = $call(@argv);
  821.         my \$context = "scalar";
  822.     };
  823.  
  824.     if ( $hints and ( ref($hints->{scalar} ) || "" ) eq 'CODE' ) {
  825.  
  826.         # We always call code refs directly, since that always
  827.         # works in 5.8.x, and always works in 5.10.1
  828.  
  829.         return $code .= qq{
  830.             if ( \$hints->{scalar}->(\$retval) ) { $die };
  831.             return \$retval;
  832.         };
  833.  
  834.     }
  835.     elsif (PERL510 and $hints) {
  836.         return $code . qq{
  837.  
  838.             if ( \$retval ~~ \$hints->{scalar} ) { $die };
  839.  
  840.             return \$retval;
  841.         };
  842.     }
  843.     elsif ( $hints ) {
  844.         croak sprintf(ERROR_58_HINTS, 'scalar', $sub);
  845.     }
  846.  
  847.     return $code .
  848.     ( $use_defined_or ? qq{
  849.  
  850.         $die if not defined \$retval;
  851.  
  852.         return \$retval;
  853.  
  854.     } : qq{
  855.  
  856.         return \$retval || $die;
  857.  
  858.     } ) ;
  859.  
  860. }
  861.  
  862. # This returns the old copy of the sub, so we can
  863. # put it back at end of scope.
  864.  
  865. # TODO : Check to make sure prototypes are restored correctly.
  866.  
  867. # TODO: Taking a huge list of arguments is awful.  Rewriting to
  868. #       take a hash would be lovely.
  869.  
  870. # TODO - BACKCOMPAT - This is not yet compatible with 5.10.0
  871.  
  872. sub _make_fatal {
  873.     my($class, $sub, $pkg, $void, $lexical, $filename, $insist) = @_;
  874.     my($name, $code, $sref, $real_proto, $proto, $core, $call, $hints);
  875.     my $ini = $sub;
  876.  
  877.     $sub = "${pkg}::$sub" unless $sub =~ /::/;
  878.  
  879.     # Figure if we're using lexical or package semantics and
  880.     # twiddle the appropriate bits.
  881.  
  882.     if (not $lexical) {
  883.         $Package_Fatal{$sub} = 1;
  884.     }
  885.  
  886.     # TODO - We *should* be able to do skipping, since we know when
  887.     # we've lexicalised / unlexicalised a subroutine.
  888.  
  889.     $name = $sub;
  890.     $name =~ s/.*::// or $name =~ s/^&//;
  891.  
  892.     warn  "# _make_fatal: sub=$sub pkg=$pkg name=$name void=$void\n" if $Debug;
  893.     croak(sprintf(ERROR_BADNAME, $class, $name)) unless $name =~ /^\w+$/;
  894.  
  895.     if (defined(&$sub)) {   # user subroutine
  896.  
  897.         # NOTE: Previously we would localise $@ at this point, so
  898.         # the following calls to eval {} wouldn't interfere with anything
  899.         # that's already in $@.  Unfortunately, it would also stop
  900.         # any of our croaks from triggering(!), which is even worse.
  901.  
  902.         # This could be something that we've fatalised that
  903.         # was in core.
  904.  
  905.         if ( $Package_Fatal{$sub} and do { local $@; eval { prototype "CORE::$name" } } ) {
  906.  
  907.             # Something we previously made Fatal that was core.
  908.             # This is safe to replace with an autodying to core
  909.             # version.
  910.  
  911.             $core  = 1;
  912.             $call  = "CORE::$name";
  913.             $proto = prototype $call;
  914.  
  915.             # We return our $sref from this subroutine later
  916.             # on, indicating this subroutine should be placed
  917.             # back when we're finished.
  918.  
  919.             $sref = \&$sub;
  920.  
  921.         } else {
  922.  
  923.             # If this is something we've already fatalised or played with,
  924.             # then look-up the name of the original sub for the rest of
  925.             # our processing.
  926.  
  927.             $sub = $Is_fatalised_sub{\&$sub} || $sub;
  928.  
  929.             # A regular user sub, or a user sub wrapping a
  930.             # core sub.
  931.  
  932.             $sref = \&$sub;
  933.             $proto = prototype $sref;
  934.             $call = '&$sref';
  935.             require autodie::hints;
  936.  
  937.             $hints = autodie::hints->get_hints_for( $sref );
  938.  
  939.             # If we've insisted on hints, but don't have them, then
  940.             # bail out!
  941.  
  942.             if ($insist and not $hints) {
  943.                 croak(sprintf(ERROR_NOHINTS, $name));
  944.             }
  945.  
  946.             # Otherwise, use the default hints if we don't have
  947.             # any.
  948.  
  949.             $hints ||= autodie::hints::DEFAULT_HINTS();
  950.  
  951.         }
  952.  
  953.     } elsif ($sub eq $ini && $sub !~ /^CORE::GLOBAL::/) {
  954.         # Stray user subroutine
  955.         croak(sprintf(ERROR_NOTSUB,$sub));
  956.  
  957.     } elsif ($name eq 'system') {
  958.  
  959.         # If we're fatalising system, then we need to load
  960.         # helper code.
  961.  
  962.         # The business with $E is to avoid clobbering our caller's
  963.         # $@, and to avoid $@ being localised when we croak.
  964.  
  965.         my $E;
  966.  
  967.         {
  968.             local $@;
  969.  
  970.             eval {
  971.                 require IPC::System::Simple; # Only load it if we need it.
  972.                 require autodie::exception::system;
  973.             };
  974.             $E = $@;
  975.         }
  976.  
  977.         if ($E) { croak ERROR_NO_IPC_SYS_SIMPLE; }
  978.  
  979.         # Make sure we're using a recent version of ISS that actually
  980.         # support fatalised system.
  981.         if ($IPC::System::Simple::VERSION < MIN_IPC_SYS_SIMPLE_VER) {
  982.             croak sprintf(
  983.             ERROR_IPC_SYS_SIMPLE_OLD, MIN_IPC_SYS_SIMPLE_VER,
  984.             $IPC::System::Simple::VERSION
  985.             );
  986.         }
  987.  
  988.         $call = 'CORE::system';
  989.         $name = 'system';
  990.         $core = 1;
  991.  
  992.     } elsif ($name eq 'exec') {
  993.         # Exec doesn't have a prototype.  We don't care.  This
  994.         # breaks the exotic form with lexical scope, and gives
  995.         # the regular form a "do or die" beaviour as expected.
  996.  
  997.         $call = 'CORE::exec';
  998.         $name = 'exec';
  999.         $core = 1;
  1000.  
  1001.     } else {            # CORE subroutine
  1002.         my $E;
  1003.         {
  1004.             local $@;
  1005.             $proto = eval { prototype "CORE::$name" };
  1006.             $E = $@;
  1007.         }
  1008.         croak(sprintf(ERROR_NOT_BUILT,$name)) if $E;
  1009.         croak(sprintf(ERROR_CANT_OVERRIDE,$name)) if not defined $proto;
  1010.         $core = 1;
  1011.         $call = "CORE::$name";
  1012.     }
  1013.  
  1014.     if (defined $proto) {
  1015.         $real_proto = " ($proto)";
  1016.     } else {
  1017.         $real_proto = '';
  1018.         $proto = '@';
  1019.     }
  1020.  
  1021.     my $true_name = $core ? $call : $sub;
  1022.  
  1023.     # TODO: This caching works, but I don't like using $void and
  1024.     # $lexical as keys.  In particular, I suspect our code may end up
  1025.     # wrapping already wrapped code when autodie and Fatal are used
  1026.     # together.
  1027.  
  1028.     # NB: We must use '$sub' (the name plus package) and not
  1029.     # just '$name' (the short name) here.  Failing to do so
  1030.     # results code that's in the wrong package, and hence has
  1031.     # access to the wrong package filehandles.
  1032.  
  1033.     if (my $subref = $Cached_fatalised_sub{$class}{$sub}{$void}{$lexical}) {
  1034.         $class->_install_subs($pkg, { $name => $subref });
  1035.         return $sref;
  1036.     }
  1037.  
  1038.     $code = qq[
  1039.         sub$real_proto {
  1040.             local(\$", \$!) = (', ', 0);    # TODO - Why do we do this?
  1041.     ];
  1042.  
  1043.     # Don't have perl whine if exec fails, since we'll be handling
  1044.     # the exception now.
  1045.     $code .= "no warnings qw(exec);\n" if $call eq "CORE::exec";
  1046.  
  1047.     my @protos = fill_protos($proto);
  1048.     $code .= $class->_write_invocation($core, $call, $name, $void, $lexical, $sub, $sref, @protos);
  1049.     $code .= "}\n";
  1050.     warn $code if $Debug;
  1051.  
  1052.     # I thought that changing package was a monumental waste of
  1053.     # time for CORE subs, since they'll always be the same.  However
  1054.     # that's not the case, since they may refer to package-based
  1055.     # filehandles (eg, with open).
  1056.     #
  1057.     # There is potential to more aggressively cache core subs
  1058.     # that we know will never want to interact with package variables
  1059.     # and filehandles.
  1060.  
  1061.     {
  1062.         no strict 'refs'; ## no critic # to avoid: Can't use string (...) as a symbol ref ...
  1063.  
  1064.         my $E;
  1065.  
  1066.         {
  1067.             local $@;
  1068.             $code = eval("package $pkg; use Carp; $code");  ## no critic
  1069.             $E = $@;
  1070.         }
  1071.  
  1072.         if (not $code) {
  1073.             croak("Internal error in autodie/Fatal processing $true_name: $E");
  1074.  
  1075.         }
  1076.     }
  1077.  
  1078.     # Now we need to wrap our fatalised sub inside an itty bitty
  1079.     # closure, which can detect if we've leaked into another file.
  1080.     # Luckily, we only need to do this for lexical (autodie)
  1081.     # subs.  Fatal subs can leak all they want, it's considered
  1082.     # a "feature" (or at least backwards compatible).
  1083.  
  1084.     # TODO: Cache our leak guards!
  1085.  
  1086.     # TODO: This is pretty hairy code.  A lot more tests would
  1087.     # be really nice for this.
  1088.  
  1089.     my $leak_guard;
  1090.  
  1091.     if ($lexical) {
  1092.  
  1093.         $leak_guard = qq<
  1094.             package $pkg;
  1095.  
  1096.             sub$real_proto {
  1097.  
  1098.                 # If we're inside a string eval, we can end up with a
  1099.                 # whacky filename.  The following code allows autodie
  1100.                 # to propagate correctly into string evals.
  1101.  
  1102.                 my \$caller_level = 0;
  1103.  
  1104.                 my \$caller;
  1105.  
  1106.                 while ( (\$caller = (caller \$caller_level)[1]) =~ m{^\\(eval \\d+\\)\$} ) {
  1107.  
  1108.                     # If our filename is actually an eval, and we
  1109.                     # reach it, then go to our autodying code immediatately.
  1110.  
  1111.                     goto &\$code if (\$caller eq \$filename);
  1112.                     \$caller_level++;
  1113.                 }
  1114.  
  1115.                 # We're now out of the eval stack.
  1116.  
  1117.                 # If we're called from the correct file, then use the
  1118.                 # autodying code.
  1119.                 goto &\$code if ((caller \$caller_level)[1] eq \$filename);
  1120.  
  1121.                 # Oh bother, we've leaked into another file.  Call the
  1122.                 # original code.  Note that \$sref may actually be a
  1123.                 # reference to a Fatalised version of a core built-in.
  1124.                 # That's okay, because Fatal *always* leaks between files.
  1125.  
  1126.                 goto &\$sref if \$sref;
  1127.         >;
  1128.  
  1129.  
  1130.         # If we're here, it must have been a core subroutine called.
  1131.         # Warning: The following code may disturb some viewers.
  1132.  
  1133.         # TODO: It should be possible to combine this with
  1134.         # write_invocation().
  1135.  
  1136.         foreach my $proto (@protos) {
  1137.             local $" = ", ";    # So @args is formatted correctly.
  1138.             my ($count, @args) = @$proto;
  1139.             $leak_guard .= qq<
  1140.                 if (\@_ == $count) {
  1141.                     return $call(@args);
  1142.                 }
  1143.             >;
  1144.         }
  1145.  
  1146.         $leak_guard .= qq< croak "Internal error in Fatal/autodie.  Leak-guard failure"; } >;
  1147.  
  1148.         # warn "$leak_guard\n";
  1149.  
  1150.         my $E;
  1151.         {
  1152.             local $@;
  1153.  
  1154.             $leak_guard = eval $leak_guard;  ## no critic
  1155.  
  1156.             $E = $@;
  1157.         }
  1158.  
  1159.         die "Internal error in $class: Leak-guard installation failure: $E" if $E;
  1160.     }
  1161.  
  1162.     my $installed_sub = $leak_guard || $code;
  1163.  
  1164.     $class->_install_subs($pkg, { $name => $installed_sub });
  1165.  
  1166.     $Cached_fatalised_sub{$class}{$sub}{$void}{$lexical} = $installed_sub;
  1167.  
  1168.     # Cache that we've now overriddent this sub.  If we get called
  1169.     # again, we may need to find that find subroutine again (eg, for hints).
  1170.  
  1171.     $Is_fatalised_sub{$installed_sub} = $sref;
  1172.  
  1173.     return $sref;
  1174.  
  1175. }
  1176.  
  1177. # This subroutine exists primarily so that child classes can override
  1178. # it to point to their own exception class.  Doing this is significantly
  1179. # less complex than overriding throw()
  1180.  
  1181. sub exception_class { return "autodie::exception" };
  1182.  
  1183. {
  1184.     my %exception_class_for;
  1185.     my %class_loaded;
  1186.  
  1187.     sub throw {
  1188.         my ($class, @args) = @_;
  1189.  
  1190.         # Find our exception class if we need it.
  1191.         my $exception_class =
  1192.              $exception_class_for{$class} ||= $class->exception_class;
  1193.  
  1194.         if (not $class_loaded{$exception_class}) {
  1195.             if ($exception_class =~ /[^\w:']/) {
  1196.                 confess "Bad exception class '$exception_class'.\nThe '$class->exception_class' method wants to use $exception_class\nfor exceptions, but it contains characters which are not word-characters or colons.";
  1197.             }
  1198.  
  1199.             # Alas, Perl does turn barewords into modules unless they're
  1200.             # actually barewords.  As such, we're left doing a string eval
  1201.             # to make sure we load our file correctly.
  1202.  
  1203.             my $E;
  1204.  
  1205.             {
  1206.                 local $@;   # We can't clobber $@, it's wrong!
  1207.                 eval "require $exception_class"; ## no critic
  1208.                 $E = $@;    # Save $E despite ending our local.
  1209.             }
  1210.  
  1211.             # We need quotes around $@ to make sure it's stringified
  1212.             # while still in scope.  Without them, we run the risk of
  1213.             # $@ having been cleared by us exiting the local() block.
  1214.  
  1215.             confess "Failed to load '$exception_class'.\nThis may be a typo in the '$class->exception_class' method,\nor the '$exception_class' module may not exist.\n\n $E" if $E;
  1216.  
  1217.             $class_loaded{$exception_class}++;
  1218.  
  1219.         }
  1220.  
  1221.         return $exception_class->new(@args);
  1222.     }
  1223. }
  1224.  
  1225. # For some reason, dying while replacing our subs doesn't
  1226. # kill our calling program.  It simply stops the loading of
  1227. # autodie and keeps going with everything else.  The _autocroak
  1228. # sub allows us to die with a vegence.  It should *only* ever be
  1229. # used for serious internal errors, since the results of it can't
  1230. # be captured.
  1231.  
  1232. sub _autocroak {
  1233.     warn Carp::longmess(@_);
  1234.     exit(255);  # Ugh!
  1235. }
  1236.  
  1237. package autodie::Scope::Guard;
  1238.  
  1239. # This code schedules the cleanup of subroutines at the end of
  1240. # scope.  It's directly inspired by chocolateboy's excellent
  1241. # Scope::Guard module.
  1242.  
  1243. sub new {
  1244.     my ($class, $handler) = @_;
  1245.  
  1246.     return bless $handler, $class;
  1247. }
  1248.  
  1249. sub DESTROY {
  1250.     my ($self) = @_;
  1251.  
  1252.     $self->();
  1253. }
  1254.  
  1255. 1;
  1256.  
  1257. __END__
  1258.  
  1259. =head1 NAME
  1260.  
  1261. Fatal - Replace functions with equivalents which succeed or die
  1262.  
  1263. =head1 SYNOPSIS
  1264.  
  1265.     use Fatal qw(open close);
  1266.  
  1267.     open(my $fh, "<", $filename);  # No need to check errors!
  1268.  
  1269.     use File::Copy qw(move);
  1270.     use Fatal qw(move);
  1271.  
  1272.     move($file1, $file2); # No need to check errors!
  1273.  
  1274.     sub juggle { . . . }
  1275.     Fatal->import('juggle');
  1276.  
  1277. =head1 BEST PRACTICE
  1278.  
  1279. B<Fatal has been obsoleted by the new L<autodie> pragma.> Please use
  1280. L<autodie> in preference to C<Fatal>.  L<autodie> supports lexical scoping,
  1281. throws real exception objects, and provides much nicer error messages.
  1282.  
  1283. The use of C<:void> with Fatal is discouraged.
  1284.  
  1285. =head1 DESCRIPTION
  1286.  
  1287. C<Fatal> provides a way to conveniently replace
  1288. functions which normally return a false value when they fail with
  1289. equivalents which raise exceptions if they are not successful.  This
  1290. lets you use these functions without having to test their return
  1291. values explicitly on each call.  Exceptions can be caught using
  1292. C<eval{}>.  See L<perlfunc> and L<perlvar> for details.
  1293.  
  1294. The do-or-die equivalents are set up simply by calling Fatal's
  1295. C<import> routine, passing it the names of the functions to be
  1296. replaced.  You may wrap both user-defined functions and overridable
  1297. CORE operators (except C<exec>, C<system>, C<print>, or any other
  1298. built-in that cannot be expressed via prototypes) in this way.
  1299.  
  1300. If the symbol C<:void> appears in the import list, then functions
  1301. named later in that import list raise an exception only when
  1302. these are called in void context--that is, when their return
  1303. values are ignored.  For example
  1304.  
  1305.     use Fatal qw/:void open close/;
  1306.  
  1307.     # properly checked, so no exception raised on error
  1308.     if (not open(my $fh, '<', '/bogotic') {
  1309.         warn "Can't open /bogotic: $!";
  1310.     }
  1311.  
  1312.     # not checked, so error raises an exception
  1313.     close FH;
  1314.  
  1315. The use of C<:void> is discouraged, as it can result in exceptions
  1316. not being thrown if you I<accidentally> call a method without
  1317. void context.  Use L<autodie> instead if you need to be able to
  1318. disable autodying/Fatal behaviour for a small block of code.
  1319.  
  1320. =head1 DIAGNOSTICS
  1321.  
  1322. =over 4
  1323.  
  1324. =item Bad subroutine name for Fatal: %s
  1325.  
  1326. You've called C<Fatal> with an argument that doesn't look like
  1327. a subroutine name, nor a switch that this version of Fatal
  1328. understands.
  1329.  
  1330. =item %s is not a Perl subroutine
  1331.  
  1332. You've asked C<Fatal> to try and replace a subroutine which does not
  1333. exist, or has not yet been defined.
  1334.  
  1335. =item %s is neither a builtin, nor a Perl subroutine
  1336.  
  1337. You've asked C<Fatal> to replace a subroutine, but it's not a Perl
  1338. built-in, and C<Fatal> couldn't find it as a regular subroutine.
  1339. It either doesn't exist or has not yet been defined.
  1340.  
  1341. =item Cannot make the non-overridable %s fatal
  1342.  
  1343. You've tried to use C<Fatal> on a Perl built-in that can't be
  1344. overridden, such as C<print> or C<system>, which means that
  1345. C<Fatal> can't help you, although some other modules might.
  1346. See the L</"SEE ALSO"> section of this documentation.
  1347.  
  1348. =item Internal error: %s
  1349.  
  1350. You've found a bug in C<Fatal>.  Please report it using
  1351. the C<perlbug> command.
  1352.  
  1353. =back
  1354.  
  1355. =head1 BUGS
  1356.  
  1357. C<Fatal> clobbers the context in which a function is called and always
  1358. makes it a scalar context, except when the C<:void> tag is used.
  1359. This problem does not exist in L<autodie>.
  1360.  
  1361. "Used only once" warnings can be generated when C<autodie> or C<Fatal>
  1362. is used with package filehandles (eg, C<FILE>).  It's strongly recommended
  1363. you use scalar filehandles instead.
  1364.  
  1365. =head1 AUTHOR
  1366.  
  1367. Original module by Lionel Cons (CERN).
  1368.  
  1369. Prototype updates by Ilya Zakharevich <ilya@math.ohio-state.edu>.
  1370.  
  1371. L<autodie> support, bugfixes, extended diagnostics, C<system>
  1372. support, and major overhauling by Paul Fenwick <pjf@perltraining.com.au>
  1373.  
  1374. =head1 LICENSE
  1375.  
  1376. This module is free software, you may distribute it under the
  1377. same terms as Perl itself.
  1378.  
  1379. =head1 SEE ALSO
  1380.  
  1381. L<autodie> for a nicer way to use lexical Fatal.
  1382.  
  1383. L<IPC::System::Simple> for a similar idea for calls to C<system()>
  1384. and backticks.
  1385.  
  1386. =cut
  1387.